import router from "./router";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from "./uilts/token";
import store from "./store";

NProgress.configure({
  showSpinner: false
})
// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  if (getToken()){
    // 如果已经登录了 还去登录那就直接进
    if (!store.state.user.userInfo.name){
      // 刷新用户信息
      await store.dispatch('getUserInfo')
      await store.dispatch('getMenu')
    }
    if (to.path==='/') {
      next('/home')
    }else {
      next()
    }
  }else {
    if (to.path==='/'){
      next()
    }else {
      next('/')
    }
  }
})

//当路由跳转结束后
router.afterEach(() => {
  // 关闭进度条
  NProgress.done()
})
